<!--
 * @Author: cgl
 * @Date: 2023-04-18 15:12:26
 * @LastEditors: cgl
 * @LastEditTime: 2023-09-05 14:16:12
 * @FilePath: \public-health-emergencies-fed:\新放射\新radiation-hygiene-fe\src\views\slide\task.vue
 * @Description:
 *
 * Copyright (c) 2023 by cgl/微盛信息产业有限公司, All Rights Reserved.
-->
<template>
  <el-dialog
    class="app-container"
    title="人员配置"
    :visible.sync="dialogVisible"
    v-dialogDrag="true"
    width="60%"
    @close="closeDialog"
  >
    <el-form
      class="searchform"
      :model="queryParams"
      ref="queryParams"
      size="small"
      label-width="80px"
      @keyup.enter.native="getList()"
    >
      <el-row :gutter="20">
        <el-col :span="6">
          <el-form-item label="用户" prop="name">
            <el-select
              v-model="queryParams.userId"
              placeholder="请选择用户"
              clearable
              style="width: 100%"
            >
              <el-option
                v-for="item in userLists"
                :key="item.userId"
                :label="item.nickName"
                :value="item.userId"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="签章类型" prop="commonSealTypes">
            <el-select
              v-model="queryParams.commonSealTypes"
              placeholder="请选择签章类型"
              multiple
              clearable
              style="width: 100%"
            >
              <el-option
                v-for="item in dict.type.common_seal_type"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label-width="20px">
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="getList()"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="reset()"
              >重置</el-button
            >
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <div class="bloperation" style="margin-bottom: 8px">
      <el-button
        class="blbutton"
        v-hasPermi="['platform:platformParticipant:add']"
        type="primary"
        size="mini"
        @click="handleAddPeople"
        >添加人员</el-button
      >
      <el-button
        class="blbutton"
        :disabled="selected.length == 0"
        v-hasPermi="['platform:platformParticipant:remove']"
        type="primary"
        size="mini"
        @click="handleRemovePeople"
        >删除</el-button
      >
    </div>
    <el-table
      ref="sendRefs"
      height="500"
      style="margin-bottom: 60px"
      v-loading="loading"
      :data="participantLists"
      @selection-change="handleSelectionChange"
      @select-all="handleSelectionAChange"
    >
      <el-table-column type="selection" align="center" width="50" />
      <el-table-column label="姓名" align="center" prop="nickName" />
      <el-table-column
        label="签章类型"
        align="center"
        prop="commonSealType"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="{ row }">
          <!-- {{ row.commonSealType }} -->
          {{ analyzeData(row) }}
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加人员 -->
    <el-dialog
      :title="bltitle"
      :visible.sync="dialogVisiblehandle"
      :close-on-click-modal="false"
      width="500px"
      class="bldialog"
      append-to-body
      v-dialogDrag="true"
      @close="closeAdd"
    >
      <el-form
        :model="addFrom"
        :rules="rules"
        ref="addParticipantRefs"
        size="small"
        label-width="130px"
      >
        <el-form-item label="用户" prop="userId">
          <el-select
            v-model="addFrom.userId"
            placeholder="请选择"
            sizi="mini"
            style="width: 100%"
          >
            <el-option
              v-for="item in userLists"
              :key="item.userId"
              :label="item.nickName"
              :value="item.userId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="签章类型" prop="commonSealTypes">
          <el-select
            v-model="addFrom.commonSealTypes"
            placeholder="请选择"
            sizi="mini"
            multiple
            style="width: 100%"
          >
            <el-option
              v-for="item in dict.type.common_seal_type"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button
          @click="
            dialogVisiblehandle = false;
            closeAdd();
          "
          >取 消</el-button
        >
      </div>
    </el-dialog>
  </el-dialog>
</template>

<script>
import {
  getTaskPersonalDosimeterParticipantList,
  getTaskPersonalDosimeterParticipantAdd,
  getTaskPersonalDosimeterParticipantDel,
} from "@/api/slides/task";
import { userListing } from "@/api/system/user";

export default {
  name: "task",
  dicts: ["common_seal_type"],
  props: {
    type: {
      type: [Number],
      default: 0,
    },
  },
  data() {
    return {
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        commonSealTypes: [],
      },
      selected: [],
      total: 0,
      dialogVisible: false,
      bltitle: "",

      participantLists: [],
      userLists: [],
      loading: false,

      dialogVisiblehandle: false,
      addFrom: {
        userId: "",
        commonSealTypes: [],
      },
      rules: {
        userId: [
          {
            required: true,
            message: "用户不能为空",
            trigger: ["blur", "change"],
          },
        ],
        commonSealTypes: [
          {
            required: true,
            message: "签章类型不能为空",
            trigger: ["blur", "change"],
          },
        ],
      },
    };
  },
  created() {},
  methods: {
    openDialog() {
      this.dialogVisible = true;
      // 获取列表
      this.getList();
      this.getUserList();
    },
    analyzeData(row) {
      const ids = row.commonSealType.split(",") || [];
      return (
        ids
          .reduce((pre, cur) => {
            pre.push(
              this.dict.type.common_seal_type.find((i) => i.value == cur).label
            );
            return pre;
          }, [])
          ?.join(",") || ""
      );
    },
    // 获取外照射任务列表
    getList() {
      this.loading = true;
      // delete this.queryParams.formDate;
      this.queryParams.commonSealType =
        this.queryParams.commonSealTypes?.join(",");
      this.queryParams.type = this.type;
      getTaskPersonalDosimeterParticipantList(this.queryParams)
        .then((response) => {
          this.participantLists = response.data || [];
          // this.total = response.total;
          this.loading = false;
        })
        .catch((response) => {
          this.loading = false;
        });
    },
    async getUserList() {
      let { code, data, msg } = await userListing();
      this.userLists = data;
      console.log(data);
    },
    handleAddPeople() {
      this.dialogVisiblehandle = true;
      this.bltitle = "添加人员";
    },
    handleRemovePeople() {
      const ids = this.selected
        .map((i) => i.taskPersonalDosimeterParticipantId)
        ?.join(",");
      console.log(ids);
      getTaskPersonalDosimeterParticipantDel(ids).then((res) => {
        this.$modal.msgSuccess("删除成功");
        this.getList();
      });
    },
    submitForm() {
      console.log(this.addFrom);
      this.addFrom.commonSealType = this.addFrom.commonSealTypes?.join(",");
      this.addFrom.type = this.type;
      getTaskPersonalDosimeterParticipantAdd(this.addFrom).then((res) => {
        this.dialogVisiblehandle = false;
        this.$modal.msgSuccess("添加成功");
        this.getList();
      });
    },
    // 查询重置
    reset() {
      this.resetForm("queryParams");
      this.getList();
    },
    // 更改打印勾选
    handleSelectionChange(selection) {
      this.selected = selection;
    },
    handleSelectionAChange(selection) {
      this.sendSelected = selection;
    },
    closeDialog() {
      this.resetForm("queryParams");
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        participantUndertakeTypes: [],
      };
    },
    closeAdd() {
      this.resetForm("addParticipantRefs");
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .handletabel.el-table {
  margin-bottom: 60px;
  overflow: auto;
  .el-table__body-wrapper {
    overflow-y: auto;
    overflow-x: hidden;
  }
}
</style>
